# 给定一个二叉树，检查它是否是镜像对称的。
#
#
#
#  例如，二叉树 [1,2,2,3,4,4,3] 是对称的。
#
#      1
#    / \
#   2   2
#  / \ / \
# 3  4 4  3
#
#
#
#
#  但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
#
#      1
#    / \
#   2   2
#    \   \
#    3    3
#
#
#
#
#  进阶：
#
#  你可以运用递归和迭代两种方法解决这个问题吗？
#  Related Topics 树 深度优先搜索 广度优先搜索
#  👍 995 👎 0

from collections import deque


# leetcode submit region begin(Prohibit modification and deletion)
# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        q = deque()

        if root:
            q.appendleft(root.left)
            q.append(root.right)
        else:
            return True

        while len(q) > 0:
            l = q.popleft()
            r = q.pop()

            if not l and not r:
                continue

            if not l or not r:
                return False

            if l.val != r.val:
                return False

            q.appendleft(l.right)
            q.appendleft(l.left)
            q.append(r.left)
            q.append(r.right)

        return True
        # leetcode submit region end(Prohibit modification and deletion)
